/*
 profilemngtTest.m
 Date Created: February 21, 2010
 
 Author: Karol Krizka <kkrizka@gmail.com>
 
 Copyright (C) 2010 Green-Power
 
 URL: http://code.google.com/p/green-power
 
 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 3 of the License, or
 (at your option) any later version.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
 $Id$
 */

#import "profilemngtTest.h"

#import "CoreDataStackHelper.h"

#import "../Classes/profilemngt/PMProfileList.h"

@implementation profilemngtTest

#if USE_APPLICATION_UNIT_TEST     // all code under test is in the iPhone Application

- (void) testAppDelegate {
    
    id yourApplicationDelegate = [[UIApplication sharedApplication] delegate];
    STAssertNotNil(yourApplicationDelegate, @"UIApplication failed to find the AppDelegate");
    
}

#else                           // all code under test must be linked into the Unit Test bundle

- (void) testProfileCreation {
  CoreDataStackHelper *cd=[[CoreDataStackHelper alloc] initCoreData];

  NSString *profile1Name=@"profile1";
  NSString *profile2Name=@"profile2";
  
  PMProfileList *profileList=[[PMProfileList alloc] initWithManagedObjectContext:cd.managedObjectContext];
  
  PMProfile *profile1=[profileList createProfile:profile1Name];
  PMProfile *profile2=[profileList createProfile:profile2Name];
  
  STAssertEquals(profile1.active,YES,@"Profile1 should be active!");
  STAssertEquals(profile2.active,NO,@"Profile2 should not be active!");
  
  int count=[[profileList listAllProfiles] count];
  STAssertEquals(2,count,@"We have created only 2 profiles!");
  
  PMProfile *profile1Check=[profileList profileForName:profile1Name];
  STAssertEquals(profile1Check,profile1,@"profileList profileForName:%@ returns profile named %@!",profile1Name,profile1Check.profileName);
  PMProfile *profile2Check=[profileList profileForName:profile2Name];
  STAssertEquals(profile2Check,profile2,@"profileList profileForName:%@ returns profile named %@!",profile2Name,profile2Check.profileName);
  
  STAssertEquals([profileList activeProfile],profile1,@"Profile 1 should be active!");
  
  [profileList release];
  
  [cd release];
}

- (void)testProfileNameUniqueness {
  CoreDataStackHelper *cd=[[CoreDataStackHelper alloc] initCoreData];
  
  NSString *profileName=@"profile1";
  
  PMProfileList *profileList=[[PMProfileList alloc] initWithManagedObjectContext:cd.managedObjectContext];
  
  STAssertTrue([profileList isProfileNameUnique:profileName],@"Profile name should be unique");
  
  PMProfile *profile1=[profileList createProfile:profileName];
  STAssertNotNil(profile1,@"Profile should be created");
  
  STAssertFalse([profileList isProfileNameUnique:profileName],@"Profile name is no longer unique");
  
  PMProfile *profile2=[profileList createProfile:profileName];
  STAssertNil(profile2,@"Profile should not be created, since the profile name is not unique");
  
  NSInteger count=[[profileList listAllProfiles] count];
  STAssertEquals(1,count,@"Only one profile should exist");
  
  PMProfile *checkProfile=[profileList profileForName:profileName];
  STAssertEquals(profile1,checkProfile,@"The first profile is the valid one");
  
  [profileList release];
  
  [cd release];
}

- (void)testProfileNameValidity {
  [self profileNameShouldBeValid:@"validprofilename"];
  [self profileNameShouldBeValid:@"valid profile name "];
  [self profileNameShouldBeValid:@"Valid Profile Name"];
  [self profileNameShouldBeValid:@"profile 1"];
  [self profileNameShouldBeValid:@"12345"];
  
  [self profileNameShouldNotBeValid:@""];
  [self profileNameShouldNotBeValid:@":@"];
  [self profileNameShouldNotBeValid:@"Recycle-Me"]; 
}

#endif

- (void)profileNameShouldBeValid:(NSString *)profileName {
  CoreDataStackHelper *cd=[[CoreDataStackHelper alloc] initCoreData];
  PMProfileList *profileList=[[PMProfileList alloc] initWithManagedObjectContext:cd.managedObjectContext];
  
  STAssertTrue([profileList isProfileNameValid:profileName],@"Profile name \"%@\" should be valid",profileName);
  
  PMProfile *profile=[profileList createProfile:profileName];
  STAssertNotNil(profile,@"Profile \"%@\" should be created",profileName);
   
  NSInteger count=[[profileList listAllProfiles] count];
  STAssertEquals(1,count,@"Only one profile should exist");
  
  PMProfile *checkProfile=[profileList profileForName:profileName];
  STAssertEquals(profile,checkProfile,@"The first profile is the valid one");

  [cd release];
}

- (void)profileNameShouldNotBeValid:(NSString *)profileName {
  CoreDataStackHelper *cd=[[CoreDataStackHelper alloc] initCoreData];
  PMProfileList *profileList=[[PMProfileList alloc] initWithManagedObjectContext:cd.managedObjectContext];
  
  STAssertFalse([profileList isProfileNameValid:profileName],@"Profile name \"%@\" should not be valid",profileName);
  
  PMProfile *profile=[profileList createProfile:profileName];
  STAssertNil(profile,@"Profile \"%@\" should not be created",profileName);
  
  NSInteger count=[[profileList listAllProfiles] count];
  STAssertEquals(0,count,@"Zero profiles should exist");

  [cd release];
}

@end
